package org.example.corpus.corpuscenter.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.corpus.corpuscenter.dao.SentenceDao;
import org.example.corpus.corpuscenter.service.SentenceService;
import org.example.corpus.model.Sentence;
import org.example.corpus.utils.SentenceSplitterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SentenceServiceImpl implements SentenceService {
    @Autowired
    private SentenceDao sentenceDao;

    @Override
    @Async
    public void processFile(String filePath, int corpusId) {
        SentenceSplitterUtil.splitSentences(filePath,corpusId, this::saveSentence);
    }

    private void saveSentence(String sentence,int corpusId) {
        try {
            Sentence corpusSentence = new Sentence();
            corpusSentence.setContent(sentence);
            corpusSentence.setCorpusId(corpusId);
            sentenceDao.insertSentence(corpusSentence);
        } catch (Exception e) {
            log.error("Failed to save sentence: {}", sentence, e);
        }
    }
}
